def get_exchange(future_code):
    """
    根据期货合约代码获取对应的交易所代码
    
    参数:
        future_code: 期货合约代码，如"FG509"
        
    返回:
        交易所代码，如"CZCE"；若无法识别则返回"未知"
    """
    # 品种代码与交易所的映射关系（包含主流活跃品种）
    exchange_map = {
        # 郑州商品交易所 (CZCE)
        'FG': 'CZCE',   # 玻璃
        'TA': 'CZCE',   # PTA
        'SR': 'CZCE',   # 白糖
        'CF': 'CZCE',   # 棉花
        'MA': 'CZCE',   # 甲醇
        'ZC': 'CZCE',   # 动力煤
        'JR': 'CZCE',   # 粳稻
        'OI': 'CZCE',   # 菜籽油
        'RM': 'CZCE',   # 菜粕
        'RS': 'CZCE',   # 菜籽
        'WH': 'CZCE',   # 强麦
        'AP': 'CZCE',   # 苹果
        'CJ': 'CZCE',   # 红枣
        'UR': 'CZCE',   # 尿素
        'PF': 'CZCE',   # 短纤
        'PK': 'CZCE',   # 
        'PX': 'CZCE',   # 
        'SA': 'CZCE',   # 
        'SM': 'CZCE',   # 
         'SH': 'CZCE',   # 
        # 大连商品交易所 (DCE)
        'm': 'DCE', # 豆粕
'y': 'DCE', # 豆油
'p': 'DCE', # 棕榈油
'c': 'DCE', # 玉米
'a': 'DCE', # 豆一
'b': 'DCE', # 豆二
'i': 'DCE', # 铁矿石
'j': 'DCE', # 焦炭
'jm': 'DCE', # 焦煤
'l': 'DCE', # 塑料
'pp': 'DCE', # 聚丙烯
'v': 'DCE', # 聚氯乙烯
'eg': 'DCE', # 乙二醇
'eb': 'DCE', # 苯乙烯
'jd': 'DCE', # 鸡蛋
'cs': 'DCE', # 玉米淀粉
        'lh': 'DCE', 
        'pg': 'DCE', 
        # 上海期货交易所 (SHFE)
        'cu': 'SHFE', # 铜
'al': 'SHFE', # 铝
'zn': 'SHFE', # 锌
'ni': 'SHFE', # 镍
'sn': 'SHFE', # 锡
'au': 'SHFE', # 黄金
'ag': 'SHFE', # 白银
'cl': 'SHFE', # 原油
'rb': 'SHFE', # 螺纹钢
'hc': 'SHFE', # 热轧卷板
'ru': 'SHFE', # 天然橡胶
'nr': 'SHFE', # 20 号胶
'fu': 'SHFE', # 燃料油
'bu': 'SHFE', # 沥青
'sp': 'SHFE', # 纸浆
'ss': 'SHFE', # 不锈钢
'ao': 'SHFE',
'ec': 'SHFE',
#广期所
        'lc':'GFEX',
        'si':'GFEX',
        'ps':'GFEX',
        #上期能源
        'sc':'INE',
        'ec':'INE',
    }
    
    # 提取品种代码（字母部分）
    product_code = ''
    for char in future_code:
        if char.isalpha():
            product_code += char
        else:
            break
    
    # 查找对应的交易所
    return exchange_map[product_code]

from datetime import datetime, timedelta
from time import time
def nanoseconds_timestamp_to_datetime(nano_timestamp):
    # 纳秒级时间戳转换为 datetime 对象
    nano_timestamp=int(nano_timestamp)
    seconds = nano_timestamp // 10**9  # 转换为秒
    nanoseconds = nano_timestamp % 10**9  # 剩余的纳秒
    dt = datetime.fromtimestamp(seconds)
    # 加上纳秒（虽然通常显示到秒或毫秒即可）
    dt = dt.replace(microsecond=nanoseconds // 1000)  # 纳秒转微秒
    timeString=dt.strftime("%H:%M:%S")
    return timeString
def getProductCode(instrumentID):
    if instrumentID[1].isalpha():
        return instrumentID[:2]
    return instrumentID[0]
PaiLieKuanDu_map={
    "cu": {
        "miniPrice": 10.0,
        "exchangeID": "SHFE",
        "name": "铜",
        "PaiLieKuanDu": 10
    },
    "al": {
        "miniPrice": 5.0,
        "exchangeID": "SHFE",
        "name": "铝",
        "PaiLieKuanDu": 10
    },
    "zn": {
        "miniPrice": 5.0,
        "exchangeID": "SHFE",
        "name": "锌",
        "PaiLieKuanDu": 10
    },
    "pb": {
        "miniPrice": 5.0,
        "exchangeID": "SHFE",
        "name": "铅",
        "PaiLieKuanDu": 10
    },
    "ni": {
        "miniPrice": 10.0,
        "exchangeID": "SHFE",
        "name": "镍",
        "PaiLieKuanDu": 10
    },
    "sn": {
        "miniPrice": 10.0,
        "exchangeID": "SHFE",
        "name": "锡",
        "PaiLieKuanDu": 10
    },
    "au": {
        "miniPrice": 0.05,
        "exchangeID": "SHFE",
        "name": "黄金",
        "PaiLieKuanDu": 10
    },
    "ag": {
        "miniPrice": 1.0,
        "exchangeID": "SHFE",
        "name": "白银",
        "PaiLieKuanDu": 30
    },
    "rb": {
        "miniPrice": 1.0,
        "exchangeID": "SHFE",
        "name": "螺纹钢",
        "PaiLieKuanDu": 6
    },
    "hc": {
        "miniPrice": 1.0,
        "exchangeID": "SHFE",
        "name": "热卷",
        "PaiLieKuanDu": 10
    },
    "wr": {
        "miniPrice": 1.0,
        "exchangeID": "SHFE",
        "name": "线材",
        "PaiLieKuanDu": 10
    },
    "fu": {
        "miniPrice": 1.0,
        "exchangeID": "SHFE",
        "name": "燃料油",
        "PaiLieKuanDu": 10
    },
    "bu": {
        "miniPrice": 1.0,
        "exchangeID": "SHFE",
        "name": "沥青",
        "PaiLieKuanDu": 10
    },
    "ru": {
        "miniPrice": 10.0,
        "exchangeID": "SHFE",
        "name": "天然橡胶",
        "PaiLieKuanDu": 10
    },
    "nr": {
        "miniPrice": 5.0,
        "exchangeID": "SHFE",
        "name": "20号胶",
        "PaiLieKuanDu": 10
    },
    "sp": {
        "miniPrice": 2.0,
        "exchangeID": "SHFE",
        "name": "纸浆",
        "PaiLieKuanDu": 10
    },
    "ao": {
        "miniPrice": 1.0,
        "exchangeID": "SHFE",
        "name": "氧化铝",
        "PaiLieKuanDu": 12
    },
    "ss": {
        "miniPrice": 5.0,
        "exchangeID": "SHFE",
        "name": "不锈钢",
        "PaiLieKuanDu": 10
    },
    "ec": {
        "miniPrice": 0.1,
        "exchangeID": "SHFE",
        "name": "欧元",
        "PaiLieKuanDu": 10
    },
    "c": {
        "miniPrice": 1.0,
        "exchangeID": "DCE",
        "name": "玉米",
        "PaiLieKuanDu": 10
    },
    "cs": {
        "miniPrice": 1.0,
        "exchangeID": "DCE",
        "name": "玉米淀粉",
        "PaiLieKuanDu": 10
    },
    "a": {
        "miniPrice": 1.0,
        "exchangeID": "DCE",
        "name": "豆一",
        "PaiLieKuanDu": 10
    },
    "b": {
        "miniPrice": 1.0,
        "exchangeID": "DCE",
        "name": "豆二",
        "PaiLieKuanDu": 10
    },
    "m": {
        "miniPrice": 1.0,
        "exchangeID": "DCE",
        "name": "豆粕",
        "PaiLieKuanDu": 10
    },
    "y": {
        "miniPrice": 2.0,
        "exchangeID": "DCE",
        "name": "豆油",
        "PaiLieKuanDu": 10
    },
    "p": {
        "miniPrice": 2.0,
        "exchangeID": "DCE",
        "name": "棕榈油",
        "PaiLieKuanDu": 10
    },
    "jd": {
        "miniPrice": 1.0,
        "exchangeID": "DCE",
        "name": "鸡蛋",
        "PaiLieKuanDu": 10
    },
    "l": {
        "miniPrice": 5.0,
        "exchangeID": "DCE",
        "name": "聚乙烯",
        "PaiLieKuanDu": 10
    },
    "v": {
        "miniPrice": 5.0,
        "exchangeID": "DCE",
        "name": "聚氯乙烯",
        "PaiLieKuanDu": 10
    },
    "pp": {
        "miniPrice": 1.0,
        "exchangeID": "DCE",
        "name": "聚丙烯",
        "PaiLieKuanDu": 10
    },
    "j": {
        "miniPrice": 0.5,
        "exchangeID": "DCE",
        "name": "焦炭",
        "PaiLieKuanDu": 10
    },
    "jm": {
        "miniPrice": 0.5,
        "exchangeID": "DCE",
        "name": "焦煤",
        "PaiLieKuanDu": 10
    },
    "i": {
        "miniPrice": 0.5,
        "exchangeID": "DCE",
        "name": "铁矿石",
        "PaiLieKuanDu": 10
    },
    "lh": {
        "miniPrice": 5.0,
        "exchangeID": "DCE",
        "name": "生猪",
        "PaiLieKuanDu": 10
    },
    "eg": {
        "miniPrice": 1.0,
        "exchangeID": "DCE",
        "name": "乙二醇",
        "PaiLieKuanDu": 10
    },
     "eb": {
        "miniPrice": 1.0,
        "exchangeID": "DCE",
        "name": "乙二醇",
        "PaiLieKuanDu": 13
    },
    "pg": {
        "miniPrice": 1.0,
        "exchangeID": "DCE",
        "name": "液化气",
        "PaiLieKuanDu": 10
    },
    "CF": {
        "miniPrice": 5.0,
        "exchangeID": "CZCE",
        "name": "棉花",
        "PaiLieKuanDu": 10
    },
    "SR": {
        "miniPrice": 1.0,
        "exchangeID": "CZCE",
        "name": "白糖",
        "PaiLieKuanDu": 10
    },
    "OI": {
        "miniPrice": 1.0,
        "exchangeID": "CZCE",
        "name": "菜籽油",
        "PaiLieKuanDu": 12
    },
    "RM": {
        "miniPrice": 1.0,
        "exchangeID": "CZCE",
        "name": "菜粕",
        "PaiLieKuanDu": 10
    },
    "TA": {
        "miniPrice": 2.0,
        "exchangeID": "CZCE",
        "name": "PTA",
        "PaiLieKuanDu": 10
    },
    "MA": {
        "miniPrice": 1.0,
        "exchangeID": "CZCE",
        "name": "甲醇",
        "PaiLieKuanDu": 10
    },
    "AP": {
        "miniPrice": 2.0,
        "exchangeID": "CZCE",
        "name": "苹果",
        "PaiLieKuanDu": 10
    },
    "CJ": {
        "miniPrice": 5.0,
        "exchangeID": "CZCE",
        "name": "红枣",
        "PaiLieKuanDu": 10
    },
    "FG": {
        "miniPrice": 1.0,
        "exchangeID": "CZCE",
        "name": "玻璃",
        "PaiLieKuanDu": 10
    },
    "PF": {
        "miniPrice": 2.0,
        "exchangeID": "CZCE",
        "name": "短纤",
        "PaiLieKuanDu": 10
    },
    "PK": {
        "miniPrice": 2.0,
        "exchangeID": "CZCE",
        "name": "花生",
        "PaiLieKuanDu": 10
    },
    "PX": {
        "miniPrice": 2.0,
        "exchangeID": "CZCE",
        "name": "对二甲苯",
        "PaiLieKuanDu": 10
    },
    "SA": {
        "miniPrice": 1.0,
        "exchangeID": "CZCE",
        "name": "纯碱",
        "PaiLieKuanDu": 10
    },
    "SM": {
        "miniPrice": 2.0,
        "exchangeID": "CZCE",
        "name": "锰硅",
        "PaiLieKuanDu": 10
    },
    "SH": {
        "miniPrice": 1.0,
        "exchangeID": "CZCE",
        "name": "硅铁",
        "PaiLieKuanDu": 10
    },
    "UR": {
        "miniPrice": 1.0,
        "exchangeID": "CZCE",
        "name": "尿素",
        "PaiLieKuanDu": 10
    },
    "lc": {
        "miniPrice": 20.0,
        "exchangeID": "GFEX",
        "name": "碳酸锂",
        "PaiLieKuanDu": 8
    },
    "si": {
        "miniPrice": 5.0,
        "exchangeID": "GFEX",
        "name": "工业硅",
        "PaiLieKuanDu": 10
    },
    "ps": {
        "miniPrice": 5.0,
        "exchangeID": "GFEX",
        "name": "聚苯乙烯",
        "PaiLieKuanDu": 10
    }
}
future_min_tick = {
    # 上海期货交易所（小写代码）
    "cu": 10.0,       # 铜
    "al": 5.0,        # 铝
    "zn": 5.0,        # 锌
    "pb": 5.0,        # 铅
    "ni": 10.0,       # 镍
    "sn": 10.0,       # 锡
    "au": 0.05,       # 黄金
    "ag": 1.0,        # 白银
    "rb": 1.0,        # 螺纹钢
    "hc": 1.0,        # 热卷
    "wr": 1.0,        # 线材
    "fu": 1.0,        # 燃料油
    "bu": 1.0,        # 沥青
    "ru": 10.0,       # 天然橡胶
    "nr": 5.0,        # 20号胶
    "sp": 2.0,        # 纸浆
    "ao": 1.0,        # 氧化铝
    "ss": 5.0,        # 不锈钢
    "ec": 0.1,        # 欧线
    
    # 大连商品交易所（小写代码）
    "c": 1.0,         # 玉米
    "cs": 1.0,        # 玉米淀粉
    "a": 1.0,         # 豆一
    "b": 1.0,         # 豆二
    "m": 1.0,         # 豆粕
    "y": 2.0,         # 豆油
    "p": 2.0,         # 棕榈油
    "jd": 1.0,        # 鸡蛋
    "l": 5.0,         # 聚乙烯
    "v": 5.0,         # 聚氯乙烯
    "pp": 1.0,        # 聚丙烯
    "eb":1.0,
    "j": 0.5,         # 焦炭（修正交易所归属）
    "jm": 0.5,        # 焦煤（修正交易所归属）
    "i": 0.5,         # 铁矿石（修正交易所归属）
    "lh": 5.0,        # 生猪
    "eg": 1.0,        # 乙二醇
    "pg": 1.0,        # 液化气
    
    # 郑州商品交易所（大写代码）
    "CF": 5.0,        # 棉花
    "SR": 1.0,        # 白糖
    "OI": 1.0,        # 菜籽油
    "RM": 1.0,        # 菜籽粕
    "TA": 2.0,        # PTA
    "MA": 1.0,        # 甲醇
    "AP": 2.0,        # 苹果
    "CJ": 5.0,        # 红枣
    "FG": 1.0,        # 玻璃
    "PF": 2.0,        # 短纤
    "PK": 2.0,        # 花生仁
    "PX": 2.0,        # 对二甲苯
    "SA": 1.0,        # 纯碱
    "SM": 2.0,        # 锰硅
    "SH": 1.0,        # 烧碱
    "UR": 1.0,        # 尿素
    
    # 广期所
    "lc": 20.0,       # 碳酸锂
    "si": 5.0,        # 工业硅
    "ps": 5.0         # 多晶硅
}


def suscribeKline(tqApi,totalDict,item,index):
    symbol=get_exchange(item)+'.'+item
    tempDict={}
    tempDict['IntrumentID']=item
    # tempDict['randomSecond']=index%30+30
    tempDict['ID']=getProductCode(item)  #字母代号
    tempDict['miniPrice']=future_min_tick[tempDict['ID']]
    tempDict['PaiLieKuanDu']=PaiLieKuanDu_map[tempDict['ID']]['PaiLieKuanDu']
    tempDict['tickObj'] = tqApi.get_tick_serial(symbol)
    tempDict['K_day'] = tqApi.get_kline_serial(symbol,86400)
    tempDict['K_h'] = tqApi.get_kline_serial(symbol,3600)
    # tempDict['K_15min'] = tqApi.get_kline_serial(symbol,60*15)
    # tempDict['K_5min'] = tqApi.get_kline_serial(symbol,60*5)
    tempDict['K_min'] = tqApi.get_kline_serial(symbol,60)
    tempDict['alertTimeDict']={}
    totalDict[item]=tempDict
    print(symbol)